package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.imp.OnPositionTListener
import com.lv.service.R
import com.lv.service.adapter.FactoryCategoryImageBottomAdapter
import com.lv.service.bean.TelSecondCategory
import com.lv.service.room.category.CategoryBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：首页找厂家分类
 */
class CategoryImagePopupView(
    private val mContext: Context, private val mTitle: String,
    private val mAllCategory: List<TelSecondCategory>? = null,
    private val topH: Int = 0,
    private var onPopSureListener: OnPositionTListener<List<TelSecondCategory>>?
) :
    BottomPopupView(mContext) {
    private val mScrollID: Int = 0
    private var mPosition = 0
    private val name = ""
    private var rvContent: RecyclerView? = null
    private var viewTop: View? = null
    private val mCategorys = mutableListOf<TelSecondCategory>()

    private var mOnPopSwitchListener: OnPositionTListener<Int>? = null

    private var mAdapter: FactoryCategoryImageBottomAdapter? = null
    override fun getImplLayoutId(): Int {
        return R.layout.category_image_factory_bottom_popup
    }

    override fun onCreate() {
        super.onCreate()
        val tv_title = findViewById<TextView>(R.id.tv_title)
        rvContent = findViewById<RecyclerView>(R.id.rv_content)
        viewTop = findViewById<View>(R.id.view_top)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }
        tv_title.text = mTitle
        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = topH
        initView()
        initEvnet()
    }


    override fun onShow() {
        super.onShow()
        initData()

    }

    private fun initView() {
        initAdapter()
        initData()

    }

    private fun initAdapter() {
        val gridLayoutManager = GridLayoutManager(context, 4)
        rvContent?.layoutManager = gridLayoutManager
        mAdapter = FactoryCategoryImageBottomAdapter(mCategorys)
        rvContent?.adapter = mAdapter
    }

    private fun initData() {
        mAllCategory?.let {
            mCategorys.clear()
            mCategorys.addAll(mAllCategory)
        }

        mAdapter?.notifyDataSetChanged()
    }


    private fun initEvnet() {

        viewTop?.setOnClickListener {
            dismiss()
        }
        mAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            val selected = !data.isSelcted
            if (selected) {
                if (mPosition == -1) {
                    mCategorys[position].isSelcted = selected
                    mAdapter?.notifyItemChanged(position, R.id.iv_image_tag)
//                    mSelectedCategorys.add(data)
                } else {
                    mCategorys[mPosition].isSelcted = !selected
                    mAdapter?.notifyItemChanged(mPosition, R.id.iv_image_tag)
//                    mSelectedCategorys.remove(mCategorys[mPosition])
//                    mSelectedCategorys.add(data)
                    mCategorys[position].isSelcted = selected
                    mAdapter?.notifyItemChanged(position, R.id.iv_image_tag)
                }
                mPosition = position


            } else {
                mCategorys[position].isSelcted = selected
                mAdapter?.notifyItemChanged(position, R.id.iv_image_tag)

            }
        }


    }

    fun scrollID(fcaid: Int) {
        val index = mCategorys.indexOfFirst { it.fcaid == fcaid }
        if (index >= 0) {
            val layoutManagerTab = rvContent?.layoutManager as LinearLayoutManager
            layoutManagerTab.scrollToPositionWithOffset(index, 0)
        }
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }


}